---
title: "Amazon EC2 | Deployment"
description: "Deploy your Mastra applications to Amazon EC2."
---

import Steps from "@site/src/components/Steps";
import StepItem from "@site/src/components/StepItem";
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# Amazon EC2

Deploy your Mastra applications to Amazon EC2 (Elastic Cloud Compute).

:::note
This guide assumes your Mastra application has been created using the default
`npx create-mastra@beta` command.
For more information on how to create a new Mastra application,
refer to our [getting started guide](/guides/v1/getting-started/quickstart)
:::

## Prerequisites

- An AWS account with [EC2](https://aws.amazon.com/ec2/) access
- An EC2 instance running Ubuntu 24+ or Amazon Linux
- A domain name with an A record pointing to your instance
- A reverse proxy configured (e.g., using [nginx](https://nginx.org/))
- SSL certificate configured (e.g., using [Let's Encrypt](https://letsencrypt.org/))
- Node.js 22.13.0 or later installed on your instance

## Deployment Steps

<Steps>

<StepItem>

Connect to your EC2 instance and clone your repository:

<Tabs>
  <TabItem value="public" label="Public Repository">

```bash copy
git clone https://github.com/<your-username>/<your-repository>.git
```

  </TabItem>

  <TabItem value="private" label="Private Repository">

```bash copy
git clone https://<your-username>:<your-personal-access-token>@github.com/<your-username>/<your-repository>.git
```

  </TabItem>
</Tabs>

Navigate to the repository directory:

```bash copy
cd "<your-repository>"
```

</StepItem>

<StepItem>

Install dependencies:

```bash copy
npm install
```

</StepItem>

<StepItem>

Create a `.env` file and add your environment variables:

```bash copy
touch .env
```

Edit the `.env` file and add your environment variables:

```bash copy
OPENAI_API_KEY=<your-openai-api-key>
# Add other required environment variables
```

</StepItem>

<StepItem>

Build the application:

```bash copy
npm run build
```

</StepItem>

<StepItem>

Run the application:

```bash copy
node --env-file=".env" .mastra/output/index.mjs
```

:::note
Your Mastra application will run on port 4111 by default. Ensure your reverse proxy is configured to forward requests to this port.
:::

</StepItem>

</Steps>

## Connect to your Mastra server

You can now connect to your Mastra server from your client application using a `MastraClient` from the `@mastra/client-js` package.

Refer to the [`MastraClient` documentation](/reference/v1/client-js/mastra-client) for more information.

```typescript copy showLineNumbers
import { MastraClient } from "@mastra/client-js";

const mastraClient = new MastraClient({
  baseUrl: "https://<your-domain-name>",
});
```

## Next steps

- [Mastra Client SDK](/reference/v1/client-js/mastra-client)
